package atm;

import java.text.SimpleDateFormat;
import java.util.Date;

import banking.Account;

public class Withdraw extends Transaction {

	public static final int TRANS_UNSTART = 1; // 未开始
	public static final int TRANS_GETDATA = 2; // 获取交易请求
	public static final int TRANS_SUCCESS = 3; // 交易成功
	public static final int TRANS_FAILURE = 4; // 交易失败
	public static final int TRANS_EXIT = 4; // 退出

	public Withdraw(Session session, Account acct) {
		super(session, acct);
		// 当选择取款交易时,需要改变显示屏的显示,需要改变数字键盘的状态

		ATM machine = ATM.getInstance();
		machine.getDisplay().setText("请输入取款金额");
		machine.getDigitButton().stateChange(1, 0, "WithdrawInfoServlet");
	}

	/**
	 * 从用户账户扣取金额
	 */
	public void execute() {
		// Account account = new Account();
		int ret = this.getAccount().withdraw(this.getAmount());
		// 扣取成功
		if (ret == 0) {
			// 显示屏更新 数字键盘状态更新
			this.setState(TRANS_SUCCESS);
			ATM machine = ATM.getInstance();
			machine.getDisplay().setText("取款成功。你的余额是" + this.getAccount().getBalance() + "<br>" + "打印:0 不打印:1");
			machine.getDigitButton().stateChange(0, 0, "WithdrawPrintServlet");
		}
		// 扣取不成功
		else if (ret == 1) {
			ATM machine = ATM.getInstance();
			machine.getDisplay().setText("取款失败，银行卡余额不足！");

		}
	}

	/**
	 * 处理打印
	 * 
	 * @param flag
	 *            0:打印 1:不打印 账号 日期 类型 金额
	 * @param session
	 */
	public void print(int flag) {
		// HttpSession session = request.getSession();
		// Session session = getSession();
		// String cardno = (String) session.getAttribute("cardNo");

		// Account account = new Account();
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");// 设置日期格式
		// 显示屏更新 数字键盘状态更新
		this.setState(TRANS_EXIT);
		this.getSession().setState(Session.CHOOSING);
		ATM machine = ATM.getInstance();
		machine.getDisplay().setText("交易账号: " + this.getAccount().getCard_no() + "<br>" + "交易日期："
				+ df.format(new Date()) + "<br>" + "交易类型:" + "取款" + "<br>" + "交易金额:" + this.getAmount());
		machine.getDigitButton().stateChange(0, 0, "TransactionServlet");

	}

}
